home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 138 / 138.xpi / chrome / stumbleupon.jar / content / signinDialog.js < prev    next >
Text File  |  2010-01-13  |  6KB  |  263 lines

  1. var detail; 
  2.  
  3. function init()
  4. {
  5.     // fill in an id if they already have one
  6.     detail = window.arguments[0];
  7.     
  8.     var id = "";
  9.     var id_list;
  10.     var splitids;
  11.     var i;
  12.     
  13.     var forgot_button = document.getElementById("stumble_signin_dialog").getButton("extra1");
  14.     
  15.     forgot_button.label = "Forgot Password";
  16.     forgot_button.setAttribute("oncommand", "forgotPassword();");
  17.  
  18.     if (opener.stumbleid == 0) 
  19.     {
  20.         // We're not logged in.  See whether we have a previous id.
  21.  
  22.         id_list = opener.su_ds.getValue("@id_list");
  23.         splitids = id_list.split(":");
  24.  
  25.         for (i = 0; i < splitids.length; i++)
  26.         {
  27.             if (splitids[i] == "")
  28.                 continue;
  29.  
  30.             id = splitids[i];
  31.             break;
  32.         }
  33.     }
  34.     else 
  35.     {
  36.         // We're logged in, so we assume that the user wants to
  37.         // switch accounts.  See whether we have a previous id.
  38.         
  39.         id_list = opener.su_ds.getValue("@id_list", "");
  40.         splitids = id_list.split(":");
  41.         var id_count = 0;
  42.         
  43.         for (i = 0; i < splitids.length; i++)
  44.         {
  45.             if (splitids[i] == "")
  46.                 continue;
  47.         
  48.             id_count++;
  49.             id = splitids[i];
  50.             if (id_count == 2)
  51.                 break;
  52.         }
  53.     }
  54.     
  55.     
  56.     if (id)
  57.     {
  58.         // they already have an account
  59.  
  60.         // see if we can get nickname
  61.         var nick = opener.su_ds.getValue("stumble." + id + ".nick", "");
  62.  
  63.         if (nick != "")
  64.             document.getElementById("username").value = nick;
  65.         else
  66.             document.getElementById("username").value = id;
  67.         document.getElementById("password").focus();
  68.     }
  69.     var autologout = opener.su_get_autologout_for_user(document.getElementById("username").value);
  70.     document.getElementById("autologout").checked = autologout;
  71. }
  72.  
  73. function forgotPassword()
  74. {
  75.     var doc = opener.getBrowser().contentDocument;
  76.     close();
  77.     doc.location = opener.su_base_url + "recover_password.php";
  78. }
  79.  
  80. function doOK()
  81. {
  82.     var username = document.getElementById("username").value;
  83.     
  84.     if (username == null)
  85.         return true;
  86.  
  87.     username = opener.su_trim(username).toLowerCase();
  88.     
  89.     if (username == "")
  90.     {
  91.         alert('Username cannot be blank');
  92.         return false;
  93.     }
  94.     
  95.     var password = document.getElementById("password").value;
  96.     if (password == null)
  97.         return true;
  98.  
  99.     if (password == "")
  100.     {
  101.         alert('Password cannot be blank');
  102.         return false;
  103.     }
  104.  
  105.     var res;
  106.     
  107.     if (opener.su_enable_hashed_password)
  108.     {
  109.         res = opener.su_post_url_server_secure(
  110.                     "userexists.php",
  111.                     "version=" + opener.su_verstring + 
  112.                         "&username=" + escape(username) + 
  113.                         "&password=" + opener.su_ds.getEncodedPassword(password, username.toLowerCase()));
  114.     }
  115.     else
  116.     {
  117.         res = opener.su_post_url_server_secure(
  118.                     "userexists.php",
  119.                     "version=" + opener.su_verstring + "&username=" + escape(username) + "&password=" + encodeURIComponent(password));
  120.     }
  121.     
  122.     if (res.status == 1)
  123.     {
  124.         alert("Cannot access stumbleupon.com at this time.\nPlease check your network connection and try again.");
  125.         return false;
  126.     }
  127.  
  128.     if (res.status != 200)
  129.     {
  130.         alert("The stumbleupon.com server is currently down.\nPlease try again, and if this occurs frequently,\ngo to http://www.stumbleupon.com/feedback.php to report the problem\nError : " + res.error + "\nStatus : " + res.status);
  131.         return false;
  132.     }
  133.  
  134.     var s = "";
  135.     if (typeof(res.response) != "undefined")
  136.         s = res.response;
  137.  
  138.     if (opener.su_log_communication)
  139.         opener.su_log("response userexists.php", s);
  140.     
  141.     var commands = s.split("\n");
  142.     // Iterate through commands
  143.     
  144.     var success = false;
  145.     var i;
  146.     for (i = 0; i < commands.length; i++)
  147.     {
  148.         if (commands[i] == "")
  149.             continue;
  150.  
  151.         // Parse command structure
  152.         var parts = commands[i].split(" ");
  153.         switch(parts[0])
  154.         {
  155.             case "ERROR":
  156.                 var msg;
  157.                 if (parts[1] == "DATABASE_FAILURE")
  158.                 {
  159.                     alert("The www.stumbleupon.com server is currently down.\nPlease try again, and if you are still having difficulties,\ngo to http://www.stumbleupon.com/feedback.php to report the problem\nError : " + ss[1]);
  160.                     return false;
  161.                 }
  162.                 else if (parts[1] == "NO_SUCH_USERNAME")
  163.                 {
  164.                     msg = "An account was not found for '" + username + "'.\n";
  165.         
  166.                     // we need to clear out that username from the list of usernames
  167.                     var id_list = opener.su_ds.getValue("@id_list");
  168.                     var splitids = id_list.split(":");
  169.                     var foundit = 0;
  170.                     var newarray = new Array();
  171.                     var line;
  172.                     for (i = 0; i < splitids.length; i++)
  173.                     {
  174.                         if (splitids[i] == "")
  175.                             continue;
  176.                 
  177.                         line = splitids[i];
  178.                         if (line == username)
  179.                             foundit = 1;
  180.                         else
  181.                             newarray.push(line);
  182.                     }
  183.                     if (foundit)
  184.                     {
  185.                         // add this id to the end
  186.                         id_list = '';
  187.                         for (i = 0; i < newarray.length; i++)
  188.                         {
  189.                             if (newarray[i] == "")
  190.                                 continue;
  191.                 
  192.                             line = newarray[i];
  193.                             id_list += line + ":";    
  194.                         }
  195.                         opener.su_ds.setValue("@id_list", id_list);
  196.                         opener.su_ds.flushPrefs();
  197.                     }            
  198.                 }
  199.                 else if (parts[1] == "INCORRECT_PASSWORD")
  200.                 {
  201.                     msg = "That password for '" + username + "' is incorrect.\n";
  202.                 }
  203.                 
  204.                 if (confirm(msg + "Would you like to recover your username/password?"))
  205.                     forgotPassword();
  206.                 
  207.                 return false;
  208.                 break;
  209.             case "USER":
  210.                 success = true;
  211.                 detail.userid = parts[1];
  212.                 detail.nickname = username;
  213.                 break;
  214.             case "NICK":
  215.                 detail.nickname = parts[1];
  216.                 break;
  217.             case "ENABLEU":
  218.                 detail.enableu = parseInt(parts[1]);
  219.                 break;
  220.             case "ENABLEG":
  221.                 detail.enableg = parseInt(parts[1]);
  222.                 break;
  223.             case "DISABLEU":
  224.                 detail.disableu = parseInt(parts[1]);
  225.                 break;
  226.             case "DISABLEG":
  227.                 detail.disableg = parseInt(parts[1]);
  228.                 break;
  229.             case "SFC":
  230.                 if(opener.su_ds.getValue("@enable_server_counts"))
  231.                     detail.thumbup_count = parseInt(parts[1]);
  232.                 break;
  233.             case "SSC":
  234.                 if(opener.su_ds.getValue("@enable_server_counts"))
  235.                     detail.stumble_count = parseInt(parts[1]);
  236.                 break;
  237.         }
  238.     }
  239.     
  240.     if (success)
  241.     {
  242.         detail.password = password;
  243.         detail.autologout = document.getElementById("autologout").checked;
  244.         opener.setTimeout(function (parent, detail) { parent.su_handle_signin_dialog_accept(detail); }, 0, opener, detail);
  245.     }
  246.     
  247.     // Other responses fail silently?
  248.     return true;
  249. }
  250.  
  251. function doCancel()
  252. {
  253.     return true;
  254. }
  255.  
  256. function handle_username_command(element)
  257. {
  258.     var autologout = opener.su_get_autologout_for_user(element.value);
  259.     
  260.     document.getElementById("autologout").checked = autologout;
  261. }
  262.  
  263.